package com.qyhl.module_practice.team.list;


import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeIsVolunteerBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeTeamBean;

import java.util.List;

public class PracticeTeamListModel implements PracticeTeamListContract.PracticeTeamListModel {
    private PracticeTeamListPresenter mPresenter;

    PracticeTeamListModel(PracticeTeamListPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getList(String page, String instId, boolean isStreet) {
        String url;
        if (isStreet) {
            url = PracticeUrl.GET_PRACTICE_INS_TEAM_LIST;
        } else {
            url = PracticeUrl.GET_PRACTICE_TEAM_LIST;
        }

        EasyHttp.get(url)
                .params("insId", instId)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("page", page)
                .execute(new SimpleCallBack<List<PracticeTeamBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何服务队！", !page.equals("1"));
                        } else {
                            mPresenter.setError("获取列表失败，请重新尝试！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeTeamBean> practiceTeamBeans) {
                        if (practiceTeamBeans != null && practiceTeamBeans.size() > 0) {
                            mPresenter.setList(practiceTeamBeans, !page.equals("1"));
                        } else {
                            mPresenter.setError("暂无任何服务队！", !page.equals("1"));
                        }
                    }
                });
    }

    @Override
    public void getVolTeamList(String volId) {
        EasyHttp.get(PracticeUrl.GET_PRACTICE_VOL_TEAM_LIST)
                .params("volId",volId)
                .execute(new SimpleCallBack<List<PracticeTeamBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何服务队！", false);
                        } else {
                            mPresenter.setError("获取列表失败，请重新尝试！", false);
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeTeamBean> practiceTeamBeans) {
                        if (practiceTeamBeans != null && practiceTeamBeans.size() > 0) {
                            mPresenter.setList(practiceTeamBeans, false);
                        } else {
                            mPresenter.setError("暂无任何服务队！", false);
                        }
                    }
                });
    }

    @Override
    public void getIsVolunteer(String phone) {

        EasyHttp.post(PracticeUrl.GET_IS_VOLUNTEER)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phone", phone)
                .execute(new SimpleCallBack<PracticeIsVolunteerBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setIsVolunteer(null);
                    }

                    @Override
                    public void onSuccess(PracticeIsVolunteerBean practiceIsVolunteerBean) {
                        mPresenter.setIsVolunteer(practiceIsVolunteerBean);
                    }
                });
    }

    @Override
    public void doSearch(String keyword, String page) {

        EasyHttp.get(PracticeUrl.GET_PRRACTICE_TEAM_SEARCH)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("name", keyword)
                .params("page", page)
                .execute(new SimpleCallBack<List<PracticeTeamBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何服务队！", !page.equals("1"));
                        } else {
                            mPresenter.setError("获取列表失败，请重新尝试！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeTeamBean> practiceTeamBeans) {
                        if (practiceTeamBeans != null && practiceTeamBeans.size() > 0) {
                            mPresenter.setSearchList(practiceTeamBeans, !page.equals("1"));
                        } else {
                            mPresenter.setError("暂无任何服务队！", !page.equals("1"));
                        }
                    }
                });
    }
}
